package com.spider.admin.store.primary.mapper;

import com.spider.admin.store.primary.model.AgentConf;
import jakarta.persistence.Transient;
import org.apache.ibatis.annotations.*;
import org.springframework.format.annotation.DateTimeFormat;

import java.util.Date;
import java.util.List;

@Mapper
public interface AgentConfMapper {
    @Select({"<script>",
            "SELECT count(0) FROM t_agent_conf_context WHERE 1=1 ",
            "<if test='confType != null'> AND conf_type = #{confType} </if>",
            "<if test='belongServerId != null'> AND belong_server_id = #{belongServerId} </if>",
            "<if test='belongAppId != null'> AND belong_app_id = #{belongAppId} </if>",
            "<if test='confVersion != null'> AND conf_version = #{confVersion} </if>",
            "<if test='startUpdateTime != null'> AND update_time BETWEEN #{startUpdateTime} AND #{endUpdateTime} </if>",
            "</script>"})
    Long queryAgentConfListCount(String confType, String belongServerId, String belongAppId, String confVersion, Date startUpdateTime, Date endUpdateTime);

    @Select({"<script>",
            "SELECT * FROM t_agent_conf_context WHERE 1=1 ",
            "<if test='confType != null'> AND conf_type = #{confType} </if>",
            "<if test='confVersion != null'> AND conf_version = #{confVersion} </if>",
            "<if test='belongServerId != null'> AND belong_server_id = #{belongServerId} </if>",
            "<if test='belongAppId != null'> AND belong_app_id = #{belongAppId} </if>",
            "<if test='startUpdateTime != null'> AND update_time BETWEEN #{startUpdateTime} AND #{endUpdateTime} </if>",
            " ORDER BY update_time DESC ",
            " limit #{pageSize} offset #{startRow} ",
            "</script>"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "conf_type", property = "confType"),
            @Result(column = "belong_server_id", property = "belongServerId"),
            @Result(column = "belong_app_id", property = "belongAppId"),
            @Result(column = "conf_context", property = "confContext"),
            @Result(column = "conf_desc", property = "confDesc"),
            @Result(column = "conf_version", property = "confVersion"),
            @Result(column = "conf_status", property = "confStatus"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<AgentConf> queryAgentConfList(int startRow, Integer pageSize, String confType, String belongServerId, String belongAppId, String confVersion, Date startUpdateTime, Date endUpdateTime);

    @Select({"<script>",
            "SELECT * FROM t_agent_conf_context WHERE (conf_status=3 OR conf_status=4) ",
            "<if test='confType != null'> AND conf_type = #{confType} </if>",
            "<if test='confVersion != null'> AND conf_version = #{confVersion} </if>",
            "<if test='belongServerId != null'> AND belong_server_id = #{belongServerId} </if>",
            "<if test='belongAppId != null'> AND belong_app_id = #{belongAppId} </if>",
            "<if test='startUpdateTime != null'> AND update_time BETWEEN #{startUpdateTime} AND #{endUpdateTime} </if>",
            " ORDER BY update_time DESC ",
            " limit #{pageSize} offset #{startRow} ",
            "</script>"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "conf_type", property = "confType"),
            @Result(column = "belong_server_id", property = "belongServerId"),
            @Result(column = "belong_app_id", property = "belongAppId"),
            @Result(column = "conf_context", property = "confContext"),
            @Result(column = "conf_desc", property = "confDesc"),
            @Result(column = "conf_version", property = "confVersion"),
            @Result(column = "conf_status", property = "confStatus"),
            @Result(column = "update_time", property = "updateTime")
    })
    List<AgentConf> queryRunAgentConfList(int i, Integer pageSize, String confType, Object o, Object o1, String confVersion, Date startUpdateTime, Date endUpdateTime);

    @Select({"<script>",
            "SELECT count(0) FROM t_agent_conf_context WHERE (conf_status=3 OR conf_status=4)  ",
            "<if test='confType != null'> AND conf_type = #{confType} </if>",
            "<if test='belongServerId != null'> AND belong_server_id = #{belongServerId} </if>",
            "<if test='belongAppId != null'> AND belong_app_id = #{belongAppId} </if>",
            "<if test='confVersion != null'> AND conf_version = #{confVersion} </if>",
            "<if test='startUpdateTime != null'> AND update_time BETWEEN #{startUpdateTime} AND #{endUpdateTime} </if>",
            "</script>"})
    Long queryRunAgentConfListCount(String confType, Object o, Object o1, String confVersion, Date startUpdateTime, Date endUpdateTime);

    @Insert({"insert into t_agent_conf_context(pid,conf_type,belong_server_id,belong_app_id,conf_context,conf_desc,conf_version,conf_status,create_time,update_time) ",
            "values(#{pid},#{confType},#{belongServerId},#{belongAppId},#{confContext},#{confDesc},#{confVersion},#{confStatus},NOW(),NOW())"})
    void insertConfContext(AgentConf agentConf);

    @Select({"SELECT * FROM t_agent_conf_context WHERE pid=#{confId} "})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "conf_type", property = "confType"),
            @Result(column = "belong_server_id", property = "belongServerId"),
            @Result(column = "belong_app_id", property = "belongAppId"),
            @Result(column = "conf_context", property = "confContext"),
            @Result(column = "conf_desc", property = "confDesc"),
            @Result(column = "conf_version", property = "confVersion"),
            @Result(column = "conf_status", property = "confStatus"),
            @Result(column = "update_time", property = "updateTime")
    })
    AgentConf getAgentConfById(String confId);

    @Update({"update t_agent_conf_context set conf_status=#{confStatus} WHERE pid=#{confId}"})
    int updateConfStatus(String confId, int confStatus);

    @Delete({"delete from t_agent_conf_context where pid=#{confId}"})
    int delAgentConf(String confId);
}
